'use strict';

angular.module('snPaas_om')
	.controller('UsersDetailCtrl',['$scope','baseUrl','userStore','appStore','SpSlider',function($scope,baseUrl,userStore,appStore,SpSlider){
		var vm = $scope;
		var userId = vm.$stateParams.id;
		
		vm.tabNow = 'env';
		vm.switchTab = function(name){
			vm.tabNow = name;
		};
		$scope.baseUrl = baseUrl.static;
		var loadData = function(){
			userStore.getDetail(userId).then(function(data){
				console.log(data);
				vm.user = data;
			});
		};
		loadData();

		vm.appStart = function(env){
			env.loading = true;
			appStore.start(vm.user.deployId,env.appId).then(function(){
				SpSlider.slideIn({
					title:'操作成功',
					content:env.appName+'启动成功',
					type:'noti'
				});
				env.loading = false;
				loadData();
			},function(){
				SpSlider.slideIn({
					title:'操作失败',
					content:env.appName+'启动失败',
					type:'alert'
				});
				env.loading = false;
				loadData();
			});
		};
		vm.appRestart = function(env){
			env.loading = true;
			appStore.restart(vm.user.deployId,env.appId).then(function(){
				SpSlider.slideIn({
					title:'操作成功',
					content:env.appName+'重启成功',
					type:'noti'
				});
				env.loading = false;
				loadData();
			},function(){
				SpSlider.slideIn({
					title:'操作失败',
					content:env.appName+'重启失败',
					type:'alert'
				});
				env.loading = false;
				loadData();
			});
		};
		vm.appStop = function(env){
			env.loading = true;
			appStore.stop(vm.user.deployId,env.appId).then(function(){
				SpSlider.slideIn({
					title:'操作成功',
					content:env.appName+'已停止',
					type:'noti'
				});
				env.loading = false;
				loadData();
			},function(){
				SpSlider.slideIn({
					title:'操作失败',
					content:env.appName+'停止失败',
					type:'alert'
				});
				env.loading = false;
				loadData();
			});
		};

		
	}]);